#!/usr/bin/python
# encoding:utf8
# cosine 2010-11-24

"""测试auth与fetch模块
"""

from kscomm.spider.auth import Auth
from kscomm.spider.fetch import HttpFetch

if __name__ == '__main__':
	# 测试的目标地址，需要Form认证
	url = 'http://t.sina.com.cn/setting/user?source=toptray'

	auth = Auth()
	auth.set_form_auth('http://login.sina.com.cn/sso/login.php',{'username':'eniyyy@gmail.com','password':'1234567'})
	
	# cookies的两种认证方式：
	# 1、urllib2 opener包含cookies认证会话，使用该opener发起请求
	# 2、通过http请求头直接发送cookies认证会话
	http_opener = auth.get_http_opener()
	cookies_auth = auth.get_cookies_auth()
	
	
	# 如果通过urllib2.install_opener方式了这个http_opener，那么将影响全局的urllib2发起的socket请求
	#auth.install_opener()
	#import test_urlopen as test
	#print test.test(url)
	
	fetch = HttpFetch()
	# cookies认证方式1
	#fetch.set_http_opener(http_opener)
	# cookies认证方式2
	fetch.set_headers(referer='',user_agent='',cookies=cookies_auth)
	fetch.request(url)
	
	html = fetch.get_html()
	if '1709710865' in html:
		print u'登录请求成功！'
	else:
		print u'登录请求失败！'
	
	
